package com.fanyue.modules.wzb.controller.admin;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.fanyue.core.annotation.CoolRestController;
import com.fanyue.core.base.BaseController;
import com.fanyue.core.request.R;
import com.fanyue.modules.base.entity.sys.BaseSysParamEntity;
import com.fanyue.modules.base.service.sys.BaseSysParamService;
import com.fanyue.modules.wzb.entity.CargoEntity;
import com.fanyue.modules.wzb.entity.ClothingBatchEntity;
import com.fanyue.modules.wzb.entity.ClothingInfoEntity;
import com.fanyue.modules.wzb.service.AppUserFaceRecordService;
import com.fanyue.modules.wzb.service.CargoService;
import com.fanyue.modules.wzb.service.ClothingBatchService;
import com.fanyue.modules.wzb.service.ClothingInfoService;
import com.fanyue.modules.wzb.vo.ClothingBatchVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestAttribute;

import java.util.ArrayList;
import java.util.List;

/**
 * 货服装信息
 */
@RequiredArgsConstructor
@Tag(name = "服装信息", description = "服装信息")
@CoolRestController(api = { "page" })
public class ClothingBatchController extends BaseController<ClothingBatchService, ClothingBatchEntity> {

    private final CargoService cargoService;
    private final ClothingInfoService clothingInfoService;
    private final AppUserFaceRecordService faceRecordService;


    @Override
    protected void init(HttpServletRequest request, JSONObject requestParams) {
    }

    /**
     * 开始批量操作调用，批量操作期间禁止刷脸，防止货位和被装混乱
     * @param requestParams
     * @return
     */
    @Operation(summary = "批量操作开关")
    @PostMapping("/replaceIsBatch")
    public R replaceIsBatch(@RequestAttribute JSONObject requestParams) {
        Integer isBatch = requestParams.getInt("clothingIsBatch");
        Integer type = requestParams.getInt("type");
        if (ObjectUtil.isEmpty(type)) {
            type = 100;
        }
        service.replaceIsBatch(isBatch);
        //归还时解除锁定
        if (isBatch == 0) {
            ClothingInfoEntity selectClothing = new ClothingInfoEntity();
            selectClothing.setStatus(9);
            selectClothing.setFaceRecordId(0L);
            CargoEntity selectCargo = new CargoEntity();
            selectCargo.setStatus(9);
            selectCargo.setFaceRecordId(0L);
            List<ClothingInfoEntity> clothingLockList = clothingInfoService.listByInfo(selectClothing);
            List<CargoEntity> cargoLockList = cargoService.listByInfo(selectCargo);
            //归还，上架
            for (ClothingInfoEntity clothing: clothingLockList) {
                if (type == 2) {
                    clothing.setStatus(4);
                } else if (type == 5) {
                    clothing.setStatus(0);
                } else {
                    clothing.setStatus(1);
                }
            }
            for (CargoEntity cargo: cargoLockList) {
                cargo.setStatus(0);
            }

            clothingInfoService.updateBatch(clothingLockList);
            cargoService.updateBatch(cargoLockList);
        }
        return R.ok();
    }

    @Operation(summary = "按条件查询被装")
    @PostMapping("/clothingListByLend")
    public R clothingListByLend(@RequestAttribute JSONObject requestParams) {
        return clothingInfoService.listByLendBatch(requestParams);
    }

    //批量借出提交
    @Operation(summary = "批量借出")
    @PostMapping("/lend")
    public R lendBatch(@RequestAttribute JSONObject requestParams) {
        return faceRecordService.lendBatch(requestParams);
    }

    //批量归还提交
    @Operation(summary = "批量归还")
    @PostMapping("/revert")
    public R revertBatch(@RequestAttribute JSONObject requestParams) {
        return faceRecordService.revertBatch(requestParams);
    }

    @Operation(summary = "外借统计")
    @PostMapping("/countByBatch")
    public R countByBatch(@RequestAttribute JSONObject requestParams) {
        return service.countByBatch(requestParams);
    }
}